<template>
  <div>
    <!-- 质量检测记录表单弹窗 -->
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="800px"
      draggable
      :align-center="true"
      @close="closeDialog"
      destroy-on-close
      style="min-width: 800px"
      class="maxhDialog"
    >
      <el-descriptions :column="2" size="large" border v-if="props.isReadonly">
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">检具编号</div>
          </template>
          {{ formData.detectionEquipCode || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">生产设备</div>
          </template>
          {{ formData.processEquipCode || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">零件编码</div>
          </template>
          {{ formData.partCode || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">特性编号</div>
          </template>
          {{ formData.paramCode || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">特性描述</div>
          </template>
          {{ formData.paramName || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">特性序号</div>
          </template>
          {{ formData.paramIndex || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">特性值</div>
          </template>
          {{ formData.paramValue || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">特性状态</div>
          </template>
          {{
            dictionaryStore.filterDictName(
              "ParamValueStatus",
              formData.paramValueStatus
            ) || ""
          }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">检测时间</div>
          </template>
          {{ formData.detectionTime || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">零件二维码</div>
          </template>
          {{ formData.partQrCode || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">检测事件</div>
          </template>
          {{ formData.detectionEvent || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">采集时间</div>
          </template>
          {{ formData.createTime || "" }}
        </el-descriptions-item>
      </el-descriptions>

      <el-form
        ref="formRef"
        v-if="!props.isReadonly"
        :model="formData"
        :rules="rules"
        label-width="160"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="检具编号" prop="detectionEquipCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.detectionEquipCode"
                placeholder="请输入检具编号"
                clearable
              />

              <span v-else>{{ formData.detectionEquipCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产设备" prop="processEquipCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.processEquipCode"
                placeholder="请输入生产设备"
                clearable
              />

              <span v-else>{{ formData.processEquipCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="零件编码" prop="partCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.partCode"
                placeholder="请输入零件编码"
                clearable
              />

              <span v-else>{{ formData.partCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性编号" prop="paramCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.paramCode"
                placeholder="请输入特性编号"
                clearable
              />

              <span v-else>{{ formData.paramCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性描述" prop="paramName">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.paramName"
                placeholder="请输入特性描述"
                clearable
              />

              <span v-else>{{ formData.paramName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性序号" prop="paramIndex">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.paramIndex"
                placeholder="请输入特性序号"
                clearable
              />

              <span v-else>{{ formData.paramIndex }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性值" prop="paramValue">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.paramValue"
                placeholder="请输入特性值"
                clearable
              />

              <span v-else>{{ formData.paramValue }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性状态" prop="paramValueStatus">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.paramValueStatus"
                placeholder="请输入特性状态"
                clearable
              />

              <span v-else>{{ formData.paramValueStatus }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测时间" prop="detectionTime">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.detectionTime"
                placeholder="请输入检测时间"
                clearable
              />

              <span v-else>{{ formData.detectionTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="零件二维码" prop="partQrCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.partQrCode"
                placeholder="请输入零件二维码"
                clearable
              />

              <span v-else>{{ formData.partQrCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测事件" prop="detectionEvent">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.detectionEvent"
                placeholder="请输入检测事件"
                clearable
              />

              <span v-else>{{ formData.detectionEvent }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采集时间" prop="createTime">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.createTime"
                placeholder="请输入采集时间"
                clearable
              />

              <span v-else>{{ formData.createTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ETLQDDETECTIONDATAADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      if (formRef.value?.clearValidate) {
        formRef.value && formRef.value.clearValidate();
      }
    }, 50);
  }
);

const rules = reactive({
  detectionEquipCode: [],
  processEquipCode: [],
  partCode: [],
  paramCode: [],
  paramName: [],
  paramIndex: [],
  paramValue: [],
  paramValueStatus: [],
  detectionTime: [],
  partQrCode: [],
  detectionEvent: [],
  createTime: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  programId: "",
  detectionEquipCode: "",
  processEquipCode: "",
  partCode: "",
  paramCode: "",
  paramName: "",
  paramIndex: "",
  paramValue: "",
  paramValueStatus: "",
  detectionTime: "",
  partQrCode: "",
  detectionEvent: "",
  createTime: "",
  dfxDataLineIndex: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        programId,
        detectionEquipCode,
        processEquipCode,
        partCode,
        paramCode,
        paramName,
        paramIndex,
        paramValue,
        paramValueStatus,
        detectionTime,
        partQrCode,
        detectionEvent,
        createTime,
        dfxDataLineIndex,
      } = newVal;
      Object.assign(formData, {
        id,
        programId,
        detectionEquipCode,
        processEquipCode,
        partCode,
        paramCode,
        paramName,
        paramIndex,
        paramValue,
        paramValueStatus,
        detectionTime,
        partQrCode,
        detectionEvent,
        createTime,
        dfxDataLineIndex,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  if (formRef.value?.resetFields) {
    formRef.value.resetFields();
    formRef.value.clearValidate();
  }
  formData.id = "";
  formData.programId = "";
  formData.detectionEquipCode = "";
  formData.processEquipCode = "";
  formData.partCode = "";
  formData.paramCode = "";
  formData.paramName = "";
  formData.paramIndex = "";
  formData.paramValue = "";
  formData.paramValueStatus = "";
  formData.detectionTime = "";
  formData.partQrCode = "";
  formData.detectionEvent = "";
  formData.createTime = "";
  formData.dfxDataLineIndex = "";
}

const loading = ref(false);

/** 质量检测记录保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/pqd-etl/qddetectiondata/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style lang="scss" scoped>
:deep(.maxhDialog .el-dialog__body) {
  max-height: 70vh;
  overflow-y: auto;
  padding: 20px;
}
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 120px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}
</style>
